package cn.itcast.service.impl;

import cn.itcast.constant.MessageConstant;
import cn.itcast.dao.OrderDao;
import cn.itcast.entity.Result;
import cn.itcast.pojo.*;
import cn.itcast.service.*;
import cn.itcast.utils.DateUtils;
import cn.itcast.vo.OrderInfoVo;
import com.alibaba.dubbo.config.annotation.Service;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.ObjectUtils;
import org.springframework.util.StringUtils;

import java.util.ArrayList;
import java.util.List;

@Service
public class OrderServiceImpl implements OrderService {

    @Autowired
    private OrderDao orderDao;
    /**
     * 下面五个导入依赖全是手机端的
     */
    @Autowired
    private OrderSettingService orderSettingService;
    @Autowired
    private MemberService memberService;
    @Autowired
    private LoginServcie loginServcie;

    @Autowired
    private CheckAddressService checkAddressService;
    @Autowired
    private SetmealService setmealService;
    //根据月份统计预约数量
    @Override
    public List<Integer> findOrderCount(List<String> list) {
        List<Integer> orderCount = new ArrayList<>();
        //遍历月份
        for (String month : list) {
            String minTime = month + ".01"; //一个月最小
            String maxTime = month + ".31";//一个月最大
            Integer orderCountdate = orderDao.findOrderCountdate(maxTime,minTime);
            orderCount.add(orderCountdate);
        }
        return orderCount;
    }

    //根据月份统计支付数量
    @Override
    public List<Integer> findPayCount(List<String> list) {
        List<Integer> payCount = new ArrayList<>();
        //遍历月份
        for (String month : list) {
            String minTime = month + ".01"; //一个月最小
            String maxTime = month + ".31";//一个月最大
            Integer payCountdate = orderDao.findPayCountdate(maxTime,minTime);
            payCount.add(payCountdate);
        }
        return payCount;
    }


    @Transactional
    @Override
    public Result checkout(OrderInfo orderInfo) {
        if (StringUtils.isEmpty(orderInfo.getOrderDate()) || StringUtils.isEmpty(orderInfo.getValidateCode()) ||
                StringUtils.isEmpty(orderInfo.getAddressId())) {
            return new Result(false, "信息不完整");
        }
        try {
            //校验是否重复预约   以身份证作为判定标准
            Member member = this.memberService.findByIdCard(orderInfo.getIdCard());
            if (null != member) {
                return new Result(false, MessageConstant.HAS_ORDERED);
            }

            //先校验今天是否有预约日期
            OrderSetting ordersetting = this.orderSettingService.
                    findByDate(DateUtils.parseString2Date(orderInfo.getOrderDate()));
            if (null == ordersetting) {
                //所选日期不能预约
                return new Result(false, MessageConstant.SELECTED_DATE_CANNOT_ORDER);
            }
            if (ordersetting.getNumber() == ordersetting.getReservations()) {
                //人数已满
                return new Result(false, MessageConstant.ORDER_FULL);
            }

            //校验验证码
            Boolean result = this.loginServcie.checkOrderCode(orderInfo.getPhoneNumber(), orderInfo.getValidateCode());
            if (!result) {

                return new Result(false, MessageConstant.VALIDATECODE_ERROR);
            }

            //修改预约人数 (事务!?)
            ordersetting.setReservations(ordersetting.getReservations() + 1);
            this.orderSettingService.update(ordersetting);
            Member member1 = new Member();
            member1.setName(orderInfo.getName());
            member1.setSex(String.valueOf(orderInfo.getSex()));
            member1.setIdCard(orderInfo.getIdCard());
            member1.setPhoneNumber(orderInfo.getPhoneNumber());
            member1.setRegTime(DateUtils.parseString2Date(orderInfo.getOrderDate()));
            member1.setStatus(1);
            this.memberService.insert(member1);
            //生成订单
            Order order = new Order();
            order.setId(orderInfo.getIdCard());
            order.setMemberId(member1.getId());
            order.setOrderDate(member1.getRegTime());
            order.setOrderType(Order.ORDERTYPE_WEIXIN);
            order.setOrderStatus(1);
            order.setSetmealId(orderInfo.getSetmealId());
            order.setArrival(0);
            order.setPhoneNumber(orderInfo.getPhoneNumber());
            order.setName(orderInfo.getName());
            order.setIdCard(orderInfo.getIdCard());
            order.setSex(member1.getSex());
            order.setAddressId(orderInfo.getAddressId());
            order.setPayStatus(1);
            //添加订单用户信息
            this.insert(order);
            return new Result(true, MessageConstant.ORDER_SUCCESS, order.getId());

        } catch (Exception e) {
            e.printStackTrace();
        }


        return null;
    }

    //添加预约订单信心
    @Override
    public Boolean insert(Order order) {
        this.orderDao.insert(order);

        return null;
    }

    /**
     * 预约成功后界面返回数据
     *
     * @param id
     * @return
     */
    @Override
    public OrderInfoVo queryOrderInfo(String id) {
        try {
            Order order = this.orderDao.findById(id);
            if (ObjectUtils.isEmpty(orderDao)) {
                return null;
            }

            OrderInfoVo orderInfoVo = new OrderInfoVo();
            orderInfoVo.setName(order.getName());
            orderInfoVo.setId(order.getId());
            orderInfoVo.setOrderDate(DateUtils.parseDate2String(order.getOrderDate()));
            orderInfoVo.setOrderType(order.getOrderType());

            Address address = this.checkAddressService.findById(order.getAddressId());
            orderInfoVo.setAddress(address);
            Setmeal setmeal = this.setmealService.findById(order.getSetmealId());
            orderInfoVo.setSetmeal(setmeal);
            return orderInfoVo;
        } catch (Exception e) {
            e.printStackTrace();
        }

        return null;
    }
}
